package com.my.ph.util;

import java.util.List;
import java.util.Map;

public class TemplateReplaceUtil {

    public static String templateReplaceNullDelete(String template, Map<String, Object> values) {
        for (Map.Entry<String, Object> entry : values.entrySet()) {
            if (entry.getValue() == null) template = template.replaceAll("◎[\\u3000]*(.*?)[\\u3000]+(\\{" + entry.getKey() +  "\\})(分钟)?\n?", "");
            else template = template.replace("{" + entry.getKey() + "}", entry.getValue().toString());
        }
        return template;
    }

    public static void main(String[] args) {
        String str = "◎集　　数　{episodesNumber}";
        System.out.println(str.replaceAll("◎[\\u3000]*(.*?)[\\u3000]+(\\{episodesNumber\\})(分钟)?\n?", ""));
    }

    public static String templateReplace(String template, Map<String, Object> values) {
        for (Map.Entry<String, Object> entry : values.entrySet()) {
            if (List.of("season", "episode").contains(entry.getKey())) template = template.replace("{" + entry.getKey() + "}", entry.getValue() == null ? "" : (entry.getValue() instanceof Integer ? String.format("%02d", entry.getValue()) : entry.getValue().toString()));
            else template = template.replace("{" + entry.getKey() + "}", entry.getValue() == null ? "" : entry.getValue().toString());
        }
        return template.replaceAll("\\.+", ".").replace(":", "：");
    }

    public static String templateReplaceAutoFeed(String template, Map<String, Object> values) {
        for (Map.Entry<String, Object> entry : values.entrySet()) {
            if (List.of("season", "episode").contains(entry.getKey())) template = template.replace("{" + entry.getKey() + "}", entry.getValue() == null ? "" : (entry.getValue() instanceof Integer ? String.format("%02d", entry.getValue()) : entry.getValue().toString()));
            else template = template.replace("{" + entry.getKey() + "}", entry.getValue() == null ? "" : entry.getValue().toString());
        }
        return template;
    }

}
